package com.hongdee.crm.tag.service.impl;

import com.hongdee.common.util.StringUtil;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.crm.Constant;
import com.hongdee.crm.tag.dao.ITCrmCarTagDao;
import com.hongdee.crm.tag.endtity.TCrmCarTag;
import com.hongdee.crm.tag.service.ITcrmCarTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by My on 2017/5/10.
 */
@Service
public class TCrmCarTagServiceImpl implements ITcrmCarTagService {

    @Autowired
    private ITCrmCarTagDao tagDao;


    /**
     * 根据上级编码 获得树信息
     * @param pTagCode
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String,Object>> getLeftTree(String pTagCode, String uid) throws Exception {
        String companyId=TokenUtil.getCompanyId(uid);
        List<Map<String,Object>> list=this.buildParamsChild(pTagCode,companyId);
        return list;
    }

    /**
     * 迭代生成树信息
     * @param code
     * @param companyId
     * @return
     * @throws Exception
     */
    public List<Map<String, Object>> buildParamsChild(String code,String companyId) throws Exception {
        String sqlStr = "select tag.* from t_crm_car_tag tag where tag.p_tag_code=?  and tag.tag_type=0 and tag.company_id=?";
        List<Map<String, Object>> child = this.tagDao.list(sqlStr,code,companyId);
        return buildDeptParent(child,companyId);
    }
    public List<Map<String, Object>> buildDeptParent(List<Map<String, Object>> paranet,String companyId) throws Exception {
        if (paranet.size() == 0) {
            return paranet;
        }
        String code;
        for (Map<String, Object> temp : paranet) {
            code = String.valueOf(temp.get("tagCode"));
            List<Map<String, Object>> childList = buildParamsChild(code,companyId);
            temp.put("child", childList);
        }
        return paranet;
    }

    /**
     * 根据父级的编码，获得子集的标签信息
     * @param start
     * @param limit
     * @param pTagCode
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public Map page(String start,String limit,String pTagCode,String uid) throws Exception {
        String companyId= TokenUtil.getCompanyId(uid);
        StringBuffer sqlStr=new StringBuffer();
        List<String> params=new ArrayList<>();
        sqlStr.append("select tag.* from t_crm_car_tag tag where tag.p_tag_code=?  and tag.company_id=?");
        params.add(pTagCode);
        params.add(companyId);
        return tagDao.page(sqlStr.toString(),params.toArray(),start,limit);
    }

    /**
     * 根据标签ID 获得标签详细信息
     * @param tagId
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public Map info(String tagId,String uid) throws Exception {
        String companyId=TokenUtil.getCompanyId(uid);
        String sql="select tag.* from t_crm_car_tag tag where tag.id=? and tag.id=? and tag.company_id=?";
        Map info = tagDao.info(sql, tagId,tagId, companyId);
        return info;
    }

    /**
     * 停用或启用标签信息
     * @param tagId
     * @param status 0停用 1启用
     * @param uid
     * @throws Exception
     */
    @Override
    public int disAndEnableTag(String tagId,String status,String uid) throws Exception {
        String companyId=TokenUtil.getCompanyId(uid);
        String sql="update t_crm_car_tag set status=? where id=? and company_id=?";
        return this.tagDao.executeSQL(sql,status,tagId,companyId);
    }

    /**
     * 修改或新增标签信息
     * @param tag
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public TCrmCarTag saveOrUpdate(TCrmCarTag tag,String uid) throws Exception {
        String companyId=TokenUtil.getCompanyId(uid);
        boolean flag=this.checkAddCode(tag.getTagCode(),companyId,tag.getId());
        if(!flag){
            if(StringUtils.isEmpty(tag.getId())){
                tag.setCompanyId(companyId);
                tag.setCreateTime(new Date().getTime());
                tag.setCreateUser(uid);
                tag.setStatus(String.valueOf(Constant.DEF_STATUS.START.ordinal()));
            }else{
                tag.setUpdateTime(new Date().getTime());
                tag.setUpdateUser(uid);
            }
            tagDao.saveOrUpdate(tag);
        }else{
            throw new Exception("标签编码重复，请调整编码信息！！");
        }
        return tag;
    }

    /**
     * 检测标签编码是否重复
     * @param code
     * @param companyId
     * @param tagId
     * @return
     * @throws Exception
     */
    public boolean checkAddCode(String code,String companyId,String tagId) throws Exception {
        StringBuffer sql=new StringBuffer();
        List<String> params=new ArrayList<>();
        sql.append(" select * from t_crm_car_tag where tag_code=? and company_id=? ");
        params.add(code);
        params.add(companyId);
        if(!StringUtils.isEmpty(tagId)){
            sql.append(" and id<>? ");
            params.add(tagId);
        }
        List list=tagDao.list(sql.toString(),params.toArray());
        if(list.size()>0){
            return true;
        }
        return false;
    }

}
